package com.gitee.wsl.struct.table.mini

import com.gitee.wsl.ext.base.Arrays.arraycopy

/**
 * 本类对应单元格的标示
 */
class CellLocation {
    var row: Int = -1
    var col: Int = -1

    // 存盘时使用
    constructor()

    constructor(cellId: String?) {
        val cl = parse(cellId)
        if (cl != null) {
            row = cl.row
            col = cl.col
        }
    }

    constructor(lct: CellLocation) {
        row = lct.row
        col = lct.col
    }

    constructor(r: Int, c: Int) {
        row = r
        col = c
    }

    fun set(row: Int, col: Int) {
        this.row = row
        this.col = col
    }

    override fun hashCode(): Int {
        return (row shl 8) + col
    }

    override fun equals(o: Any?): Boolean {
        if (this === o) {
            return true
        }
        if (o is CellLocation) {
            if (o.row == row && o.col == col) {
                return true
            }
        }
        return false
    }

    override fun toString(): String {
        return getCellId(row, col)!!
    }



    companion object {
        private const val serialVersionUID: Long = 0x00010001

        private const val MAX_COLCHAR_COUNT = 6

        fun toRow(row: Int): String? {
            var row = row
            val ret = CharArray(12)
            var j = 12

            if (row < 0) {
                return null
            } else if (row == 0) {
                ret[--j] = '0'
            } else {
                while (row > 0) {
                    ret[--j] = ('0'.code + row % 10).toChar()
                    row /= 10
                }
            }
            return ret.concatToString(j, j + (12 - j))
        }

        fun toCol(col: Int): String? {
            var col = col
            val ret = CharArray(MAX_COLCHAR_COUNT)
            var i = MAX_COLCHAR_COUNT
            if (col < 0) {
                return null
            } else if (col == 0) {
                ret[--i] = '`'
            } else {
                while (col > 0) {
                    ret[--i] = ('A'.code + (col - 1) % 26).toChar()
                    col = (col - 1) / 26
                }
            }

            return ret.concatToString(i, i + (MAX_COLCHAR_COUNT - i))
        }

        /**
         * 将字符串转成列号
         * @param id String
         * @return int
         */
        fun parseCol(id: String?): Int {
            if (id == null) return -1
            val len = id.length
            if (len == 0) return -1

            var col = 0
            for (i in 0 until len) {
                val c = id[i]
                if (c !in 'A'..'Z') {
                    return -1
                }
                col = col * 26 + (c.code - 'A'.code) + 1
            }
            return col
        }

        /**
         * 将字符串转成行号
         * @param id String
         * @return int
         */
        fun parseRow(id: String?): Int {
            if (id == null) return -1
            val len = id.length
            if (len == 0) return -1

            var row = 0
            for (i in 0 until len) {
                val c = id[i]
                if (c !in '0'..'9') {
                    return -1
                }
                row = row * 10 + (c.code - '0'.code)
            }
            return row
        }

        //单元格的标示转换为行号列号
        fun parse(id: String?): CellLocation? {
            if (id == null) {
                return null
            }
            val length = id.length
            if (length < 2) {
                return null
            }

            val rowSize = 0x7fffffffL
            val colSize = 0x7fff

            var i = 0
            var row: Long = 0
            var col = 0
            var c = 0.toChar()
            var flag = false

            i = 0
            while (i < length) {
                c = id[i]
                if (c !in 'A'..'Z') {
                    break
                }
                col = col * 26 + (c.code - 'A'.code) + 1
                if (col > colSize) {
                    return null
                }
                flag = true
                i++
            }
            if (c == '`' && i == 0) {
                flag = true
                i++
            }
            if (!flag) {
                return null
            }
            flag = false
            while (i < length) {
                c = id[i]
                if (c !in '0'..'9') {
                    return null
                }
                row = row * 10 + (c.code - '0'.code)
                if (row > rowSize) {
                    return null
                }
                flag = true
                i++
            }
            if (!flag) {
                return null
            }
            return CellLocation(row.toInt(), col)
        }

        /**
         * 根据行号列号转换为单元格的标示
         * @param row int
         * @param col int
         * @return String
         */
        fun getCellId(row: Int, col: Int): String? {
            var row = row
            var col = col
            val ret = CharArray(16)
            var i = 8
            var j = 16
            if (col < 0) {
                return null
            } else if (col == 0) {
                ret[--i] = '`'
            } else {
                while (col > 0) {
                    ret[--i] = ('A'.code + (col - 1) % 26).toChar()
                    col = (col - 1) / 26
                }
            }
            if (row < 0) {
                return null
            } else if (row == 0) {
                ret[--j] = '0'
            } else {
                while (row > 0) {
                    ret[--j] = ('0'.code + row % 10).toChar()
                    row /= 10
                }
            }
            arraycopy(ret, j, ret, 8, 16 - j)
            return ret.concatToString(i, i + (8 - i + 16 - j))
        }
    }
}
